$(function () {
    layui.use(['laydate', 'laypage', 'form'], function () {
            let laypage = layui.laypage;
            //分页完整功能
            laypage.render({
                elem: 'demo7',
                count: getNum(),
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                jump: function (obj) {
                    let pageDatas = {"count": obj.count, "curr": obj.curr, "pages": obj.pages, "limit": obj.limit};

                    $.ajax({
                        url: "http://localhost:8080/back/news/findAll",
                        dataType: "json",
                        /*设置请求头*/
                        headers:{"Authorization":"Bearer "+localStorage.getItem("jwtToken")},
                        data: {"page": obj.curr, "limit": obj.limit},
                        success: function (res) {
                            var html;
                            // console.log("新闻页面数据：", res)
                            for (var i = 0; i < res.data.length; i++) {
                                html += ' <tr>\n' +
                                    '                                    <td>\n' +
                                    '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
                                    '                                    </td>\n' +
                                    '                                    <td>' + res.data[i].id + '</td>\n' +
                                    '                                    <td>' + res.data[i].title + '</td>\n' +
                                    '                                    <td>' + res.data[i].typeName + '</td>\n' +
                                    '                                    <td>' + (res.data[i].isShow == 1 ? "热点" : "/") + '</td>\n' +
                                    '                                    <td>' + (res.data[i].isRecommend == 1 ? "置顶" : "/") + '</td>\n' +
                                    '                                    <td class="td-manage">\n' +
                                    '                                      <a title="编辑"  onclick="sendInfo(' + res.data[i].id + ')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                      <a title="删除" onclick="memberdel(this,' + res.data[i].id + ')" href="javascript:;">\n' +
                                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                                    '                                      </a>\n' +
                                    '                                    </td>\n' +
                                    '                                  </tr>'

                            }
                            $("#newsInfo").html(html);

                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            /*用户未登录*/
                            if (jqXHR.status == 403) {
                                window.location.href = "http://localhost:63342/spring-boot-front-page/html/login.html"
                            }

                        }
                    })
                }
            })
        }
    )
})


//得到记录条数
function getNum() {
    let count = 0;
    $.ajax({
        url: "http://localhost:8080/back/news/count",
        dataType: "json",
        /*设置请求头*/
        headers:{"Authorization":"Bearer "+localStorage.getItem("jwtToken")},
        async: false,//关闭异步
        success: function (res) {
            count = res.data;

        },
        error: function (jqXHR, textStatus, errorThrown) {
            /*用户未登录*/
            if (jqXHR.status == 403) {
                window.location.href = "http://localhost:63342/spring-boot-front-page/html/login.html"
            }

        }
    })
    return count;
}


//编辑新闻
function sendInfo(id) {
    layui.use(['layer'], function () {
        let layer = layui.layer;
        sessionStorage.setItem("newsid", id);

        xadmin.open('编辑', '../../html/news/news-edit.html', 600, 400);

    })
}

//删除某一行用户
function memberdel(obj, id) {
    layui.use(['layer'], function () {
        let news = {"id": id}
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "http://localhost:8080/back/news/delete",
                type: "post",
                dataType: "json",
                /*设置请求头*/
                headers: {"username": localStorage.getItem("loginUser")},
                data: JSON.stringify(news),
                contentType: "application/json",
                success: function (res) {
                    if (res.msg == "success") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('删除失败!', {icon: 1, time: 1000});
                    }


                },
                error: function (jqXHR, textStatus, errorThrown) {
                    /*用户未登录*/
                    if (jqXHR.status == 403) {
                        window.location.href = "http://localhost:63342/spring-boot-front-page/html/login.html"
                    }

                }
            })
        });
    })
}

